<?php
	$error = "";
	$msg = "";
	$fileElementName = 'upload';
	if(!empty($_FILES[$fileElementName]['error']))
	{
		switch($_FILES[$fileElementName]['error'])
		{

			case '1':
				$error = 'The uploaded file exceeds the upload_max_filesize directive in php.ini';
				break;
			case '2':
				$error = 'The uploaded file exceeds the MAX_FILE_SIZE directive that was specified in the HTML form';
				break;
			case '3':
				$error = 'The uploaded file was only partially uploaded';
				break;
			case '4':
				$error = 'No file was uploaded.';
				break;

			case '6':
				$error = 'Missing a temporary folder';
				break;
			case '7':
				$error = 'Failed to write file to disk';
				break;
			case '8':
				$error = 'File upload stopped by extension';
				break;
			case '999':
			default:
				$error = 'No error code avaiable';
		}
	}elseif(empty($_FILES['upload']['tmp_name']) || $_FILES['upload']['tmp_name'] == 'none')
	{
		$error = 'No file was uploaded..';
	}else 
	{
			
			$name = uniqid().'_'.$_FILES['upload']['name'];
			$dir = dirname($_FILES['upload']['tmp_name']);
			$path = $dir.'/'.$name;
			
			if(!move_uploaded_file($_FILES['upload']['tmp_name'], $path)) {
				$msg .= " Cannot move uploaded file";
			}
			
			$msg .= " File Name: " . $name . ", ";
			$msg .= " File Size: " . filesize($path);
			$msg .= " File Path: " . addslashes($path);
			
			//for security reason, we force to remove all uploaded file
			//@unlink($_FILES['upload']);		
	}
	
	echo "{";
	echo				"error: '" . $error . "',\n";
	echo				"msg: '" . $msg . "',\n";
	echo				"name: '" . addslashes($name) . "',\n";
	echo				"path: '" . addslashes($path) . "'\n";
	echo "}";
?>